const { StructuredSearchService } = require('./build/structured-search');
const path = require('path');

async function testStructuredSearchWithDirectory() {
  console.log('=== Testing Structured Search with Directory Parameter ===\n');
  
  const structuredSearch = new StructuredSearchService();
  const testDirectory = path.join(__dirname, 'example-docs');
  
  try {
    console.log('1. Testing with specific directory path...');
    console.log(`   Search directory: ${testDirectory}`);
    
    // 测试1：在指定目录中搜索
    const results1 = await structuredSearch.searchByMetadata('typescript', testDirectory);
    console.log(`   Found ${results1.length} results for "typescript" in example-docs:`);
    results1.forEach((result, index) => {
      console.log(`   ${index + 1}. ${path.basename(result.filePath)}`);
      console.log(`      Metadata: ${JSON.stringify(result.metadata, null, 2).slice(0, 100)}...`);
    });
    
    console.log('\n2. Testing with default directory (current working directory)...');
    // 测试2：使用默认目录
    const results2 = await structuredSearch.searchByMetadata('typescript');
    console.log(`   Found ${results2.length} results for "typescript" in current directory:`);
    results2.forEach((result, index) => {
      console.log(`   ${index + 1}. ${path.basename(result.filePath)}`);
    });
    
    console.log('\n3. Testing Chinese search with specific directory...');
    // 测试3：中文搜索
    const results3 = await structuredSearch.searchByMetadata('中文', testDirectory);
    console.log(`   Found ${results3.length} results for "中文" in example-docs:`);
    results3.forEach((result, index) => {
      console.log(`   ${index + 1}. ${path.basename(result.filePath)}`);
      console.log(`      Metadata: ${JSON.stringify(result.metadata, null, 2).slice(0, 100)}...`);
    });

    console.log('\n4. Testing specific field search with directory...');
    // 测试4：特定字段搜索
    const fieldQueries = { 'title': 'guide' };
    const results4 = await structuredSearch.searchBySpecificFields(fieldQueries, testDirectory);
    console.log(`   Found ${results4.length} results for title containing "guide" in example-docs:`);
    results4.forEach((result, index) => {
      console.log(`   ${index + 1}. ${path.basename(result.filePath)}`);
      console.log(`      Title: ${result.metadata.title || 'No title'}`);
    });
    
    console.log('\n✅ All structured search tests completed successfully!');
    
  } catch (error) {
    console.error('❌ Test failed:', error);
    process.exit(1);
  }
}

testStructuredSearchWithDirectory();
